package controllers.remberme

import jp.t2v.lab.play2.auth.{AuthenticityToken, CookieTokenAccessor}
import play.api.mvc.{Cookie, RequestHeader}
import play.api.mvc.Result

/**
 * Created by xinliwang on 15/9/20.
 */
class RememberMeTokenAccessor(maxAge: Int) extends CookieTokenAccessor() {

  override def put(token: AuthenticityToken)(result: Result)(implicit request: RequestHeader): Result = {
    val remember = request.tags.get("rememberme").contains("true") || request.session.get("rememberme").contains("true")
    val _maxAge = if (remember) Some(maxAge) else None
    val c = Cookie(cookieName, sign(token), _maxAge, cookiePathOption, cookieDomainOption, cookieSecureOption, cookieHttpOnlyOption)
    result.withCookies(c)
  }


}
